#include <bits/stdc++.h>

using namespace std;

class Solution
{
public:
    int bestTeamScore(vector<int> &scores, vector<int> &ages)
    {
        vector<pair<int, int>> s;
        for (int i = 0; i < scores.size(); ++i)
        {
            s.push_back({scores[i], ages[i]});
        }
        sort(s.begin(), s.end());
        vector<int> dp(scores.size(), 0);
        int ret = 0;
        // 前面的球员成绩低，所以年龄可以比当前球员的年龄小或相等
        for (int i = 0; i < scores.size(); ++i)
        {
            dp[i] = s[i].first;
            for (int j = 0; j < i; ++j)
            {
                if (s[j].second <= s[i].second)
                {
                    dp[i] = max(dp[i], dp[j] + s[i].first);
                }
            }
            ret = max(ret, dp[i]);
        }
        return ret;
    }
};